package com.horizon.mobile.util;

import java.io.File;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.horizon.mobile.R;

public class FilePreviewUtil {
	
	private Context mContext;
	
	 ProgressDialog mProgressDialog;
		
	
	public FilePreviewUtil(Context context){
		
		this.mContext = context;
	}



	public void showContent(String filepath, Context context) {
		File currentPath = new File(filepath);
		try {
			if (currentPath != null && currentPath.isFile()) {
				String fileName = currentPath.toString();
				Intent intent;
				if (checkEndsWithInStringArray(fileName, context.getResources().getStringArray(R.array.fileEndingImage))) {
					intent = OpenFiles.getImageFileIntent(currentPath);
					context.startActivity(intent);
				} else if (checkEndsWithInStringArray(fileName, context.getResources().getStringArray(R.array.fileEndingWebText))) {
					intent = OpenFiles.getHtmlFileIntent(currentPath);
					context.startActivity(intent);
				} else if (checkEndsWithInStringArray(fileName, context.getResources().getStringArray(R.array.fileEndingPackage))) {
					intent = OpenFiles.getApkFileIntent(currentPath);
					context.startActivity(intent);
				} else if (checkEndsWithInStringArray(fileName, context.getResources().getStringArray(R.array.fileEndingAudio))) {
					intent = OpenFiles.getAudioFileIntent(currentPath);
					context.startActivity(intent);
				} else if (checkEndsWithInStringArray(fileName, context.getResources().getStringArray(R.array.fileEndingVideo))) {
					intent = OpenFiles.getVideoFileIntent(currentPath);
					context.startActivity(intent);
				} else if (checkEndsWithInStringArray(fileName, context.getResources().getStringArray(R.array.fileEndingText))) {
					intent = OpenFiles.getTextFileIntent(currentPath);
					context.startActivity(intent);
				} else if (checkEndsWithInStringArray(fileName, context.getResources().getStringArray(R.array.fileEndingPdf))) {
					intent = OpenFiles.getPdfFileIntent(currentPath);
					context.startActivity(intent);
				} else if (checkEndsWithInStringArray(fileName, context.getResources().getStringArray(R.array.fileEndingWord))) {
					intent = OpenFiles.getWordFileIntent(currentPath);
					context.startActivity(intent);
				} else if (checkEndsWithInStringArray(fileName, context.getResources().getStringArray(R.array.fileEndingExcel))) {
					intent = OpenFiles.getExcelFileIntent(currentPath);
					context.startActivity(intent);
				} else if (checkEndsWithInStringArray(fileName, context.getResources().getStringArray(R.array.fileEndingPPT))) {
					intent = OpenFiles.getPPTFileIntent(currentPath);
					context.startActivity(intent);
				} else {
					showToast(context, "无法打开，请安装相应的软件！");
					
				}
			} else {
				showToast(context, "对不起，这不是文件！");
			}

		} catch (ActivityNotFoundException e) {
			showToast(context, "无法打开，请安装相应的软件！");
			e.printStackTrace();
		} catch (IllegalStateException e) {
			showToast(context, "怎么回事！");
			e.printStackTrace();
		} catch (Exception e) {
			showToast(context, "出错了！");
			e.printStackTrace();
		}
	}
	
	public void showToast(Context context,String value){
		Toast.makeText(context, value, Toast.LENGTH_SHORT).show();
	}
	@SuppressLint("DefaultLocale")
	public boolean checkEndsWithInStringArray(String checkItsEnd, String[] fileEndings) {
		for (String aEnd : fileEndings) {
			if (checkItsEnd.toLowerCase().endsWith(aEnd))
				return true;
		}
		return false;
	}
	
	
	
	public void showProgressDialog(String msg) {
		if (mProgressDialog == null) {
			mProgressDialog = new ProgressDialog(mContext);
			mProgressDialog.setCancelable(true);
			mProgressDialog.setMessage(msg);
		}
		if(!mProgressDialog.isShowing())
		mProgressDialog.show();
	}
	public void showProgressDialog(String msg ,boolean cancleble) {
		if (mProgressDialog == null) {
			mProgressDialog = new ProgressDialog(mContext);
			mProgressDialog.setCancelable(cancleble);
			mProgressDialog.setMessage(msg);
		}
		if(!mProgressDialog.isShowing())
		mProgressDialog.show();
	}
	/*
	 * 闅愯棌杩涘害妗�	 */
	public void cancelProgressDialog() {
		if(mProgressDialog!=null&&mProgressDialog.isShowing())
		mProgressDialog.cancel();
	}

	
}
